package com.example.localbites;

import java.util.List;

import com.example.constants.StringConstant;
import com.example.yelp.service.beans.Business;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class RestaurantAdapter extends ArrayAdapter<Business> 
{
	private int resource;
    private LayoutInflater inflater;
    private Context ctx;
    private List<Business> resultData;
    private StringConstant con = new StringConstant();
    
	public RestaurantAdapter(Context context, int resourceId, List<Business> objects) {
		super(context, resourceId, objects);
		
		resource = resourceId; 
		inflater = LayoutInflater.from(context);
		ctx = context;
		resultData = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		Log.d("RestaurantAdapter", "Inside getView function");
				
		/* create a new view of my layout and inflate it in the row */
        convertView = (RelativeLayout) inflater.inflate( resource, null );

        /* Extract the city's object to show */
        final Business restaurantData = resultData.get( position );
        
        if(restaurantData == null)
        {
        	return null;
        }
       
        if(restaurantData.getDisplayAddress()== null)
        {
        	if(restaurantData.getLocation() != null)
        	{
        		String address = restaurantData.getLocation().getDisplayAddress().toString();
        		address = address.replace("[", "");
        		address = address.replace("]", "");
        		       		
        		restaurantData.setDisplayAddress(address);
        	}
        }
        
        if(restaurantData.getLattitude()== 0.0)
        {
        	if(restaurantData.getLocation() != null)
        	{
        		if(restaurantData.getLocation().getCoordinate() != null)
        		{
        			restaurantData.setLattitude(restaurantData.getLocation().getCoordinate().getLatitude());
        		}
        		
        	}
        }
        
        if(restaurantData.getLongitude()== 0.0)
        {
        	if(restaurantData.getLocation() != null)
        	{
        		if(restaurantData.getLocation().getCoordinate() != null)
        		{
        			restaurantData.setLongitude(restaurantData.getLocation().getCoordinate().getLongitude());
        		}
        		
        	}
        }
       //  Take the TextView from layout and set the restaurant's name 
        TextView txtName = (TextView) convertView.findViewById(R.id.restaurantname);
        txtName.setText(restaurantData.getName());
        txtName.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				//Start new restaurant_details activity
				Intent intent = new Intent(ctx,RestaurantDetailsActivity.class);
				intent.putExtra(con.RESTAURANT_DETAILS,restaurantData);
				ctx.startActivity(intent);
			}
		});
       //  Take the TextView from layout and set the restaurant Distance 
        TextView txtDistance = (TextView) convertView.findViewById(R.id.restaurantDistance);
        txtDistance.setText(Double.toString(restaurantData.getDistance()/1609).substring(0, 4) +"mi");
        txtDistance.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//Toast.makeText(ctx,"Inside List Activity", Toast.LENGTH_SHORT).show();
				//Start new restaurant_details activity
				//intent = new Intent(this, RestaurantList.class);
				Intent intent = new Intent(ctx,RestaurantDetailsActivity.class);
				intent.putExtra(con.RESTAURANT_DETAILS,restaurantData);
				ctx.startActivity(intent);
			}
		});
        /* Take the ImageView from layout and set the city's image 
        ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity);
        String uri = "drawable/" + restaurantData.getImageUrl();
        int imageResource = ctx.getResources().getIdentifier(uri, null, ctx.getPackageName());
        Drawable image = ctx.getResources().getDrawable(imageResource);
        imageCity.setImageDrawable(image);*/
        Log.d("RestaurantAdapter", "Finished getView function");
        return convertView;
	}
}
